package cn.yusei.teamTribe.controller.user;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.TextType;
import cn.yusei.teamTribe.constant.UsernameType;
import cn.yusei.teamTribe.entity.User;
import cn.yusei.teamTribe.service.TextFilterService;
import cn.yusei.teamTribe.service.user.TokenService;
import cn.yusei.teamTribe.service.user.UserService;
import cn.yusei.teamTribe.util.PhoneUtil;
import cn.yusei.teamTribe.util.ResponseUtil;
import cn.yusei.teamTribe.util.StringUtil;
import cn.yusei.teamTribe.util.TokenUtil;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private TokenService tokenService;
	@Autowired
	private TextFilterService textFilterService;
	
	@Value("${helloName}")
	private String helloName;
	@Value("${usernameLength}")
	private int usernameLength;
	@Value("${passwordLength}")
	private int passwordLength;
	@Value("${tokenName}")
	private String tokenName;
	
	/**
	 * 测试
	 * @param out
	 */
	@RequestMapping("/hello")
	public void hello(
			PrintWriter out)
	{
		out.print("<html>");
		out.print("Hello!" + helloName + "<br>");
		out.println("</html>");
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 * @param usernameType		用户名类型，如手机、邮箱。暂时必须为0
	 */
	@RequestMapping(value="/user/login", method=RequestMethod.POST)
	public void login(
			@RequestParam String username,
			@RequestParam String password,
			@RequestParam int usernameType,
			PrintWriter out)
	{
		username = textFilterService.getFilterString(username, TextType.NICKNAME);
		if(StringUtil.haveNullOrEmpty(username, password))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(!UsernameType.isUsernameType(usernameType))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(StringUtil.lengthAbove(username, usernameLength) || StringUtil.lengthAbove(password, passwordLength))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setUsernameType(usernameType);
		try
		{
			int code = userService.login(user);
			if(code != ResponseCode.REQUEST_SUCCESSED)
			{
				ResponseUtil.responseCode(out, code);
				return;
			}
			Map<String, Object> param = new HashMap<String, Object>();
			String token = TokenUtil.getToken(user.getId());		//如果登录成功，user的id将被UserService赋值
			tokenService.addToken(user.getId(), token);
			param.put(tokenName, token);
			param.put("id", user.getId());
			ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/user/logout")
	public void logout(@RequestParam("TeamTribeToken") String token)
	{
		tokenService.deleteToken(token);
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 */
	@RequestMapping(value="/user/register", method=RequestMethod.POST)
	public void register(
			@RequestParam String username,
			@RequestParam String password,
			@RequestParam String phone,
			@RequestParam String validateCode,
			PrintWriter out)
	{
		if(StringUtil.haveNullOrEmpty(username, password, phone, validateCode))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(StringUtil.lengthAbove(username, usernameLength) || StringUtil.lengthAbove(password, passwordLength))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(!PhoneUtil.isPhoneNumber(phone)){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		try
		{
			int code = userService.register(user, phone, validateCode);
			ResponseUtil.responseCode(out, code);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
		}
	}
	
}
